package com.tom.zx.service.impl;

import com.tom.zx.entity.Contract;
import com.tom.zx.entity.DecorationScheme;
import com.tom.zx.entity.FurnishOrder;
import com.tom.zx.entity.HouseInformation;
import com.tom.zx.mapper.ContractMapper;
import com.tom.zx.mapper.DecorationSchemeMapper;
import com.tom.zx.mapper.FurnishOrderMapper;
import com.tom.zx.mapper.HouseInformationMapper;
import com.tom.zx.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author lp
 * @since 2022-04-09
 */
@Service
@AllArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    private final HouseInformationMapper houseInformationMapper;
    private final DecorationSchemeMapper decorationSchemeMapper;

    private final FurnishOrderMapper furnishOrderMapper;
    @Override
    @Transactional
    public boolean saveOrUpdate2(Contract contract) {
        final Long houseId = contract.getHouseId();
        final Long decorationSchemeId = contract.getDecorationSchemeId();
        final HouseInformation houseInformation = houseInformationMapper.selectById(houseId);
        final DecorationScheme decorationScheme = decorationSchemeMapper.selectById(decorationSchemeId);
        //创建订单
        final FurnishOrder furnishOrder = new FurnishOrder();
        furnishOrder.setOrderNumber(UUID.randomUUID().toString().replace("_",""));
        furnishOrder.setSquareMeter(houseInformation.getSize());
        furnishOrder.setOrderUnitPrice(decorationScheme.getUnitPrice());
        furnishOrder.setStatus(0);
        furnishOrder.setUserId(houseInformation.getUserId());
        furnishOrder.setOrderSum(furnishOrder.getOrderUnitPrice().multiply(new BigDecimal(furnishOrder.getSquareMeter())));
        furnishOrderMapper.insert(furnishOrder);
        return saveOrUpdate(contract);
    }
}
